package yxy.game.pm2.module.battle;

import java.util.concurrent.TimeUnit;

import org.joda.time.DateTime;
import org.redisson.api.RBucket;

import yxy.apple.util.redisson.RedisException;

public class BattleModule extends BattleRecord {

	private static  int expireDay = 7 ;//过期时间

	// bean===========================================================================

	private String playerCode;

	private boolean finished;// 战斗完成
	private boolean settled;// 已结算
	private int step;// 战斗步数
	// 结算
	private ISettlement settlement;

	public void setPlayerCode(String playerCode) {
		this.playerCode = playerCode;
	}

	public String getPlayerCode() {
		return playerCode;
	}

	public boolean isFinished() {
		return finished;
	}

	public void setFinished(boolean finished) {
		this.finished = finished;
	}

	public boolean isSettled() {
		return settled;
	}

	public void setSettled(boolean settled) {
		this.settled = settled;
	}

	public int getStep() {
		return step;
	}

	public void setStep(int step) {
		this.step = step;
	}

	public ISettlement getSettlement() {
		return settlement;
	}

	public void setSettlement(ISettlement settlement) {
		this.settlement = settlement;
	}

	// func===========================================================================

//	public void battle() {
//		Team team_a = getTeamA().team(true);
//		Team team_b = getTeamB().team(false);
//		team_a.setTeam对手(team_b);
//		team_b.setTeam对手(team_a);
//		Battle battle = new Battle();
//		battle.battle(team_a, team_b);
//
//		setRecord(battle.get记录().getBattle().toJSONString());
//		setStatistics(battle.get记录().get统计().getFighters());
//		setBattleResult(battle.get记录().getResult());
//		setRound(battle.getContext().getRound());
//		setFinished(true);
//		set();
//	}

	public boolean battling() {
		if (!isFinished() || isSettled()) {
			return false;
		}
		if (getTimeline() == null) {
			return false;
		}
//		System.err.println("战斗::\t" + timeline().getEndTime().toString(DateTimeFormat.forPattern("HH:mm:ss")) + "\n现在::\t"	+ DateTime.now().toString(DateTimeFormat.forPattern("HH:mm:ss")));
		return timeline().getEndTime().minusSeconds(5).isAfter(DateTime.now());// 5秒阈值
	}

	public BattleRecord saveRecord(String recordCode) {
		return saveRecord(recordCode, 7);
	}

	public BattleRecord saveRecord(String recordCode, int expireDays) {
		String code = getCode();
		setCode(recordCode);
		setv(expireDays);

		setCode(code);
		return BattleRecord.getv(recordCode);
	}

	// redis==========================================================================

	private static String key(String playerCode, String battleCode) {
		return String.format("PLAYER:%s:BATTLE:%s", playerCode, battleCode);
	}

	public void set(int expireDay) {
		try {
			RBucket<BattleRecord> bucket = touch(key(getPlayerCode(), getCode()));
			bucket.set(this);
			if (expireDay > 0) {
				bucket.expire(expireDay, TimeUnit.DAYS);
			}
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查记录并重新设置7天过期
	 * @param playerCode
	 * @param battleCode
	 * @return
	 */
	static public BattleModule getAndSave(String playerCode, String battleCode) {
		try {
			RBucket<BattleModule> bucket = touch(key(playerCode, battleCode));
			BattleModule module = bucket.get();
			if (module == null) {
				return null;
			}
			//7天过期
			bucket.expire(expireDay, TimeUnit.DAYS);
			return module;
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}

	static public BattleModule get(String playerCode, String battleCode) {
		try {
			RBucket<BattleModule> bucket = touch(key(playerCode, battleCode));
			BattleModule module = bucket.get();
			if (module == null) {
				return null;
			}
			return module;
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}

	static public BattleModule create(String playerCode) {
		BattleModule module = new BattleModule();
		module.setPlayerCode(playerCode);

		return module;
	}



	//从redis移走数据
	public static void removeAllFromRedis(String playerCode,String battleCode) throws RedisException {
		try {
			touch(key(playerCode, battleCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	/** 计算时间 */
	public void time() {
		DateTime end = timeline().getStartTime().plusSeconds(getStep() * 7 / 10 + 2);// 战斗步数 x 0.7秒
		timeline().setEndTime(end);
	}
}
